package pl.mcnova.view.panels;

import java.text.Format;

import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.swingx.JXDatePicker;

import pl.mcnova.model.consts.MailsConsts;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

//TODO : subject + data(JXDatePicker x2 + (contractors ?)

public class MailFilterPanel extends JPanel {

	public static final String[] phoneOptions = new String[] { "Filtruj", "Anuluj" };
	private JLabel contractor;
	private JLabel subject;
	private JTextField contractorBox;
	private JTextField subjectBox;
	private JLabel dateFrom;
	private JLabel dateTo;
	private JXDatePicker dateFromPicker;
	private JXDatePicker dateToPicker;
	private CellConstraints cc;

	public MailFilterPanel() {

		FormLayout layout = new FormLayout("pref,4dlu, 100dlu, 4dlu, min",
				"pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref, 2dlu, pref");

		setLayout(layout);
		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Filtrowanie maili"),
				BorderFactory.createEmptyBorder(0, 6, 6, 6)));

		init();
		setComponents();
	}

	public void setComponents() {
		cc = new CellConstraints();

		add(contractor, cc.xy(1, 3));
		add(contractorBox, cc.xy(3, 3));
		add(subject, cc.xy(1, 5));
		add(subjectBox, cc.xy(3, 5));
		add(dateFrom, cc.xy(1, 7));
		add(dateFromPicker, cc.xy(3, 7));
		add(dateTo, cc.xy(1, 9));
		add(dateToPicker, cc.xy(3, 9));

	}

	public void init() {
		contractor = createLabel(MailsConsts.contractor + " : ");
		contractorBox = createTextField();
		subject = createLabel(MailsConsts.subject + " : ");
		subjectBox = createTextField();
		dateFrom = createLabel(MailsConsts.dateFrom + " : ");
		dateTo = createLabel(MailsConsts.dateTo + " : ");
		dateFromPicker = new JXDatePicker();
		dateToPicker = new JXDatePicker();

	}

	public void clear() {
		contractorBox.setText("");
		subjectBox.setText("");
		dateFromPicker.setDate(null);
		dateToPicker.setDate(null);

	}

	public JLabel createLabel(String name) {
		JLabel label = new JLabel(name);

		return label;
	}

	public JTextField createTextField() {
		JTextField text = new JTextField(20);

		return text;

	}

	public JFormattedTextField createFormattedTextField(Format format) {
		JFormattedTextField text = new JFormattedTextField(format);

		return text;
	}

	public String getSubject() {
		return subjectBox.getText();
	}

	public String getContractor() {
		return contractorBox.getText();
	}

	public Long getDateFrom() {
		if (dateFromPicker.getDate() != null) return dateFromPicker.getDate().getTime();
		return 0L;
	}

	public Long getDateTo() {
		if (dateToPicker.getDate() != null) return dateToPicker.getDate().getTime();
		return Long.MAX_VALUE;
	}

}
